package com.example.wechat_applet.filter;

import com.example.wechat_applet.mapper.UserMapper;
import com.example.wechat_applet.pojo.LoginUser;
import com.example.wechat_applet.pojo.Role;
import com.example.wechat_applet.pojo.User;
import com.example.wechat_applet.service.UserService;
import com.example.wechat_applet.utils.JwtUtil;
import com.example.wechat_applet.utils.RedisCache;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Component
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
    @Autowired
    private RedisCache redisCache;
    
    @Autowired
    private UserService userService;

    @Autowired
    UserMapper userMapper;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException
    {
        //filterChain.doFilter(request,response);

        //获取token
        String token = request.getHeader("token");
        if(Objects.isNull(token))//如果没有获取到token，就直接放行
        {
            filterChain.doFilter(request,response);
            return;
        }
        //解析token
        String userid = null;
        try
        {
            Claims claims = JwtUtil.parseJWT(token);
            userid = claims.getSubject();
        }
        catch (Exception e)
        {
//            throw new AccessDeniedException("权限不足，无法访问!");
            //return;
            throw new RuntimeException("token解析失败");
        }
//        //从redis中获取用户信息
//        String redisKey = "login:"+userid;
//        LoginUser loginUser = redisCache.getCacheObject(redisKey);
//        if(Objects.isNull(loginUser))
//        {
//            throw new RuntimeException("用户未登录");
//        }

        User user = userService.getById(userid);
        LoginUser loginUser = new LoginUser(user);
        System.out.println(loginUser);
        List<Role> roleNames = userMapper.haveRole(user.getId());
        loginUser.setRoleList(roleNames);

        //存放SecurityContextHolder
        //TODO 获取权限信息封装到
//        User loginUser = userService.getById(userid);
        
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(loginUser,null,loginUser.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
        filterChain.doFilter(request,response);
    }

}
